package com.easypan.controller;

import com.easypan.Service.FileInfoService;
import com.easypan.annotation.GlobalInterceptor;
import com.easypan.entity.dto.SessionwebUserDto;
import com.easypan.entity.po.FileInfo;
import com.easypan.entity.query.FileInfoQuery;
import com.easypan.entity.vo.PaginationResultVO;
import com.easypan.entity.vo.R;
import com.easypan.enums.FileCategoryEnums;
import com.easypan.enums.FileDelFlagEnums;
import com.easypan.mapper.FileInfoMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/file")
public class fileController extends CommonFileController{
    @Resource
    private FileInfoService fileInfoService;



    @PostMapping("/loadDataList")
 @GlobalInterceptor
    public R list(HttpSession session, FileInfoQuery query, String category)
{
    //为后面指定查询埋下伏笔吗
    FileCategoryEnums fileCategoryEnums = FileCategoryEnums.getByCode(category);
    if (null != fileCategoryEnums) {
        //拿到类别对应的数字表示
        query.setFileCategory(fileCategoryEnums.getCategory());
    }
//没值就是空--查询所有类型
    SessionwebUserDto sessionKey = (SessionwebUserDto) session.getAttribute("session_key");
   if(sessionKey==null)
   {
       R.error("用户错误");
   }
   //给予userid
    query.setUserId(sessionKey.getUserId());
    //从枚举中获取对应的标记，FileDelFlagEnums中 ->查找的是正常文件 不是回收站的
    query.setDelFlag(FileDelFlagEnums.USING.getFlag());
    //设置为2 查询 正常状态文件
    query.setOrderBy("last_update_time desc");
    PaginationResultVO PaginationResultVO=fileInfoService.findListByPage(query);
   //分页查询的封装

    return R.success(PaginationResultVO);

    /*
SELECT file_id, user_id, file_md5, file_pid, file_size, file_name, file_cover, file_path, create_time, last_update_time, folder_type, file_category, file_type, status, recovery_time, del_flag
FROM file_info f
WHERE user_id = 1
  AND file_pid = 0
  AND del_flag = 2
ORDER BY last_update_time DESC
LIMIT 0,10


     */
}

@PostMapping("/uploadFile")
@GlobalInterceptor
public R uploadFile(HttpSession session, String fileId, MultipartFile file,
                    String fileName,String filePid,
                    String fileMd5,Integer chunkIndex,Integer chunks) {
//
    SessionwebUserDto sessionKey = (SessionwebUserDto) session.getAttribute("session_key");

    R re=fileInfoService.updat(sessionKey,fileId,file,fileName,
            filePid,fileMd5,chunkIndex, chunks,session);
    return re;
}

    @RequestMapping("/getImage/{imageFolder}/{imageName}")

    public R xianshitp(HttpSession
            session, @PathVariable("imageFolder")String imageFolder,
                       @PathVariable("imageName") String imageName, HttpServletResponse httpServletResponse) throws IOException {
       super.getImage(httpServletResponse,session,imageFolder,imageName);
        return R.success("成功");
    }

@GetMapping("/ts/getVideoInfo/{fileId}")
@GlobalInterceptor
    public R spbf(HttpServletResponse httpServletResponse,@PathVariable("fileId") String fileId,HttpSession session)
{
    R file = super.getFile(httpServletResponse, fileId, session);
    return file;
}
@RequestMapping("/getFile/{id}")
@GlobalInterceptor
    public R getfile(@PathVariable("id") String id,HttpServletResponse httpServletResponse,HttpSession session)

{

    R file = super.getFile(httpServletResponse, id, session);
    return file;

}

    @PostMapping("/newFoloder")
    @GlobalInterceptor
    public R newFoloder(HttpSession session, String filePid , String fileName)
    {

        R zs= fileInfoService.newFoloder(session,filePid,fileName);
        return zs;
    }
    @RequestMapping("/getFolderInfo")
    @GlobalInterceptor
    //前端 o4lxjztfYX / yFKPxNy6yZ 层级目录 --返回目录集合 -来排序
    public R getFolderInfo(HttpSession session,String path)
    {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
        return super.getFolderInfo(path, webUserDto.getUserId());
    }

    @PostMapping("/rename")
    @GlobalInterceptor
    public  R rename(HttpSession session,String fileId,String fileName)
    {
     ///1.思路 改库 ->
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
    R file= fileInfoService.rename(webUserDto,fileId,fileName);

        return file;
    }
    ///移动文件夹
    //	http://easypan.wuhancoder.com/api/file/loadAllFolder
    //先写获取文件夹接口 ->会给一个filepid->0 ->目录下包含的所有文件夹 不知道需不需要递归
    @PostMapping("/loadAllFolder")
    public R loadAllFolder(String filePid,String currentFileIds,HttpSession session)
    {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
      R r= fileInfoService.loadAllFolder(webUserDto,filePid,currentFileIds);

        return r;
    }


    //    接口状态: 开发中
    //    接口URL: POSThttp://localhost:7090/api/file/changeFileFolder
    //    Content-Type: multipart/form-data
    //    认证方式:无需认证
    @PostMapping("/changeFileFolder")
    @GlobalInterceptor
    //too :对方伪造参数 不匹配的user +文件 ID 文件 --
    public  R changeFileFolder(String fileIds,String filePid,HttpSession session)
    {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
         R r   = fileInfoService.changeFileFolder(fileIds,filePid,webUserDto);
        return r;
    }
   ///    接口状态: 开发中
    //    接口URL: GEThttp://localhost:7090/api/file/createDownloadUrl/{fileId}
    //    Content-Type: multipart/form-data
    //    认证方式:无需认证
 ///先创建下载链接,此步骤执行结束之后会生成一个code,通过code进行下载操作,这个code拥有时效性
    ///     * 这是防止被恶意盗取下载
    @PostMapping ("/createDownloadUrl/{fileid}")
    @GlobalInterceptor
    public  R createDownloadUrl(@PathVariable("fileid") String fileId,HttpSession session)
    {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
       R r= fileInfoService.createDownloadUrl(fileId,webUserDto);
        //思路 - 下载链接 --
        return r;
    }
    //http://easypan.wuhancoder.com/api/file/download/RMrid4jiybSxAB23VBkUwPgrHDqRI5eHhHvrzErFmQBfMKXhj3
    @GetMapping("/download/{code}")
    @GlobalInterceptor
    public R download(@PathVariable("code")String code, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
        R r=fileInfoService.download(request,response,code);
        return r;
    }
    ///    接口状态: 开发中
    //    接口URL: POSThttp://localhost:7090/api/file/delFile
    //    Content-Type: multipart/form-data
    //    认证方式:无需认证
    @PostMapping("/delFile")
    @GlobalInterceptor
    public  R delFile(String fileIds,HttpSession session)
    {
        SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
            //删除 ---是不是 程序自动删除 写一个守护进程--隔一个周期就删除一下嘛
          R r= fileInfoService.delFile(webUserDto,fileIds);
        return r;
    }

///too 图片后缀应该不区分大小写
}
